﻿using OASystem.BLL.IService;
using OASystem.DAL;
using OASystem.Model;
using OASystem.ViewModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace OASystem.BLL.Service
{
    //记得修改Userinfo成自己要使用的实体
    public class MailListService : BaseRepository<Maillist>, IMailListService
    {
        BaseRepository<Maillist> mail = new BaseRepository<Maillist>();
        BaseRepository<Departinfo> depart = new BaseRepository<Departinfo>();
        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <param name="username"></param>
        /// <returns></returns>
        public ResponseResult MaiPageList(int page, int limit, string username,int departid)
        {
            ResponseResult responseResult = new ResponseResult();
            List<Maillist> maillists = new List<Maillist>();
            responseResult.code = 0;
            responseResult.msg = string.Empty;
            int count = 0;
            Expression<Func<Maillist, bool>> whereLambda = a => true;
            if (!string.IsNullOrEmpty(username))
            {
                whereLambda = a => a.Pinyin.Contains(username);
            }
            else if(departid!=0)
            {
                whereLambda = a => a.DepartId==departid;
            }
            Expression<Func<Maillist, int>> orderbyLambda = c => c.Id;
             maillists = QueryPageList(page, limit, whereLambda, orderbyLambda, out count);
            responseResult.data = maillists;
            responseResult.count = count;
            if (username!=null)
            {
                List<Maillist> maillists1 = new List<Maillist>();
                int a = 0;
                //查询出集合赋值给maillists
                //循环遍历maillists
                for (int i = 0; i < maillists.Count; i++)
                {
                    //接收截取拼音首字母
                    string Pinyins = maillists[i].Pinyin.Substring(0, 1);
                    //判断是否一致
                    if (Pinyins == username)
                    {
                        a++;
                        //responresult对象接受
                        maillists1.Add(maillists[i]);
                    }
                }
                responseResult.data = maillists1;
                responseResult.count = a;
            }
          
            return responseResult;
        }

        /// <summary>
        /// 单表删除
        /// </summary>
        /// <param name="maillist"></param>
        /// <returns></returns>
        public ResponseResult MailDel(Maillist maillist)
        {
            ResponseResult responseResult = new ResponseResult();
            responseResult.data = mail.Delete(maillist);
            return responseResult;
        }
        /// <summary>
        /// 显示编辑数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ResponseResult Update(int id)
        {
            ResponseResult responseResult = new ResponseResult();

            Expression<Func<Maillist, bool>> whereLambda = a => a.Id == id;
            if (Query(whereLambda) != null)
            {
                responseResult.success = true;
            }

            responseResult.data = Query(whereLambda);
            return responseResult;
        }
        /// <summary>
        /// 编辑通讯录
        /// </summary>
        /// <param name="maillist"></param>
        /// <returns></returns>
        public ResponseResult MailUpdate(Maillist maillist)
        {
            ResponseResult responseResult = new ResponseResult();
            responseResult.data = mail.Update(maillist);
            return responseResult;
        }
        /// <summary>
        /// 查询部门集合
        /// </summary>
        /// <param name="username"></param>
        /// <returns></returns>
        public ResponseResult QueryDepart(string username)
        {
            ResponseResult responseResult = new ResponseResult();
            responseResult.code = 0;
            responseResult.msg = string.Empty;
            int count = 0;
            Expression<Func<Departinfo, bool>> whereLambda = a => true;
            if (!string.IsNullOrEmpty(username))
            {
                whereLambda = a => a.DepartName.Equals(username);
            }
            responseResult.data = depart.QueryList(whereLambda);
            responseResult.count = count;
            return responseResult;
        }
        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ResponseResult MoreDel(string id)
        {
            string[] strArray = id.Split(',');
            int[] iNums;
            //string转换int[]
            iNums = Array.ConvertAll(strArray, int.Parse);
            ResponseResult result = new ResponseResult();
            result.data = BatchDeleteById(iNums);
            return result;
        }
    }
}
